package com.biostime.dingdingbox.modules.db.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.Box;
import com.biostime.dingdingbox.modules.db.service.AlbumService;
import com.biostime.dingdingbox.modules.db.service.BoxService;
import com.biostime.dingdingbox.modules.sys.entity.User;
import com.biostime.dingdingbox.modules.db.entity.Album;


/**
 * 玩具盒类型表Controller
 * 
 * @author chenweicheng
 * @version 2016-06-25
 */
@Controller
@RequestMapping(value = "${adminPath}/db/box")
public class BoxController extends BaseController {

	@Autowired
	private BoxService boxService;

	@Autowired
	private AlbumService albumService;

	@ModelAttribute
	public Box get(@RequestParam(required = false) String id) {
		Box entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = boxService.get(id);
		}
		if (entity == null) {
			entity = new Box();
		}
		return entity;
	}


	
	
	@RequiresPermissions("db:box:view")
	@RequestMapping(value = { "list", "" })
	public String List(Box box, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		Page<Box> page = boxService.findPage(new Page<Box>(request, response),
				box);
		
		model.addAttribute("page", page);
		return "modules/db/boxList";

	}

	@RequiresPermissions("db:box:view")
	@RequestMapping(value = "form")
	public String form(Box box, Model model) {
		if (box.getId() != null && box.getId() != "") {
			Album ca = new Album();
			ca.setCategory("box");
			ca.setObjectId(StringUtils.toInteger(box.getId()));
			List<Album> boxAlbum = albumService.findList(ca);
			model.addAttribute("boxAlbum", boxAlbum);
		}
		model.addAttribute("box", box);
		return "modules/db/boxForm";
	}

	@RequiresPermissions("db:box:edit")
	@RequestMapping(value = "save")
	public String save(Box box, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		if (!beanValidator(model, box)) {
			return form(box, model);
		}
		if (box != null) {
			box.setCreateBy(new User("1"));
			box.setUpdateBy(new User("1"));
			box.setCreateDate(new Date());
			box.setUpdateDate(new Date());
			box.setDelFlag("0");
		}
		boxService.save(box);

		Album cla = new Album();
		cla.setCategory("box");
		cla.setObjectId(StringUtils.toInteger(box.getId()));
		albumService.deleteAll(cla);

		String[] photos = request.getParameterValues("hid_photo_name");
		String[] remarks = request.getParameterValues("hid_photo_remark");
		if (photos != null) {
			for (int i = 0; i < photos.length; i++) {
				String photo = photos[i];
				String remark = remarks[i];

				if (!StringUtils.isEmpty(photo)) {
					String[] parts = photo.split("\\|");
					if (parts != null && parts.length >= 6) {
						Album album = new Album();
						album.setObjectId(StringUtils.toInteger(box.getId()));
						album.setCategory(parts[0]);
						album.setFileName(parts[1]);
						album.setOrginPath(parts[2]);
						album.setThumbPath(parts[3]);
						album.setAlbumSize(StringUtils.toInteger(parts[4]));
						album.setRemarks(remark);
						albumService.save(album);
					}
				}
			}
		}

		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/db/box/?repage";
	}

	@RequiresPermissions("db:box:edit")
	@RequestMapping(value = "delete")
	public String delete(
			@RequestParam(value = "ids", required = false) String idsRequest,
			Box box, RedirectAttributes redirectAttributes) {
		// 批量删除
		if (!StringUtils.isEmpty(idsRequest)) {
			String[] ids = idsRequest.split(",");
			for (String id : ids) {
				boxService.delete(new Box(id));
			}
		} else {
			boxService.delete(box);
		}

		addMessage(redirectAttributes, "删除成功");
		return "redirect:" + Global.getAdminPath() + "/db/box/?repage";
	}

	@RequestMapping(value = "show")
	public String show(Box box, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		List<Box> boxList = boxService.show();
		model.addAttribute("boxList", boxList);
		return "modules/front/demo2";
	}

	@RequestMapping(value = "do")
	public String todo(Box box, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		List<Box> boxList2 = boxService.todo();
		model.addAttribute("boxList2", boxList2);
		return "modules/front/demo3";
	}
}